package com.ruoyi.pwk.service.impl;

import java.util.*;

import com.ruoyi.base.domain.BasePipenetworkassociationtable;
import com.ruoyi.base.domain.BusWaterSegment;
import com.ruoyi.base.domain.BusWaterSegmentorrhoutfall;
import com.ruoyi.base.mapper.BasePipenetworkassociationtableMapper;
import com.ruoyi.base.mapper.BusWaterSegmentMapper;
import com.ruoyi.base.mapper.BusWaterSegmentattributeMapper;
import com.ruoyi.base.mapper.BusWaterSegmentorrhoutfallMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.monitor.domain.BusWaterAutomonitorHourdata;
import com.ruoyi.monitor.mapper.BusWaterAutomonitorHourdataMapper;
import com.ruoyi.pollution.domain.BusPsBasWateroutput;
import com.ruoyi.pollution.domain.BusPsMidWaterhourdata;
import com.ruoyi.pollution.mapper.BusPsBasWateroutputMapper;
import com.ruoyi.pollution.mapper.BusPsMidWaterhourdataMapper;
import com.ruoyi.pwk.domain.BusPwkRhoutfallData;
import com.ruoyi.pwk.mapper.BusPwkRhoutfallDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pwk.mapper.BusPwkRhoutfallMapper;
import com.ruoyi.pwk.domain.BusPwkRhoutfall;
import com.ruoyi.pwk.service.IBusPwkRhoutfallService;
import org.springframework.util.CollectionUtils;

/**
 * 入河排污口Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class BusPwkRhoutfallServiceImpl implements IBusPwkRhoutfallService 
{
    @Autowired
    private BusPwkRhoutfallMapper busPwkRhoutfallMapper;
    @Autowired
    private BasePipenetworkassociationtableMapper basePipenetworkassociationtableMapper;
    @Autowired
    private BusPwkRhoutfallDataMapper busPwkRhoutfallDataMapper;
    @Autowired
    private BusPsBasWateroutputMapper busPsBasWateroutputMapper;
    @Autowired
    private BusPsMidWaterhourdataMapper busPsMidWaterhourdataMapper;
    @Autowired
    private BusWaterSegmentorrhoutfallMapper busWaterSegmentorrhoutfallMapper;
    @Autowired
    private BusWaterSegmentMapper busWaterSegmentMapper;
    @Autowired
    private BusWaterAutomonitorHourdataMapper busWaterAutomonitorHourdataMapper;

    /**
     * 查询入河排污口
     * 
     * @param id 入河排污口主键
     * @return 入河排污口
     */
    @Override
    public BusPwkRhoutfall selectBusPwkRhoutfallById(String id)
    {
        return busPwkRhoutfallMapper.selectBusPwkRhoutfallById(id);
    }

    /**
     * 查询入河排污口列表
     * 
     * @param busPwkRhoutfall 入河排污口
     * @return 入河排污口
     */
    @Override
    public List<BusPwkRhoutfall> selectBusPwkRhoutfallList(BusPwkRhoutfall busPwkRhoutfall)
    {
        return busPwkRhoutfallMapper.selectBusPwkRhoutfallList(busPwkRhoutfall);
    }

    /**
     * 新增入河排污口
     * 
     * @param busPwkRhoutfall 入河排污口
     * @return 结果
     */
    @Override
    public int insertBusPwkRhoutfall(BusPwkRhoutfall busPwkRhoutfall)
    {
        busPwkRhoutfall.setId(UUID.randomUUID().toString());
        return busPwkRhoutfallMapper.insertBusPwkRhoutfall(busPwkRhoutfall);
    }

    /**
     * 修改入河排污口
     * 
     * @param busPwkRhoutfall 入河排污口
     * @return 结果
     */
    @Override
    public int updateBusPwkRhoutfall(BusPwkRhoutfall busPwkRhoutfall)
    {
        return busPwkRhoutfallMapper.updateBusPwkRhoutfall(busPwkRhoutfall);
    }

    /**
     * 批量删除入河排污口
     * 
     * @param ids 需要删除的入河排污口主键
     * @return 结果
     */
    @Override
    public int deleteBusPwkRhoutfallByIds(String[] ids)
    {
        return busPwkRhoutfallMapper.deleteBusPwkRhoutfallByIds(ids);
    }

    @Override
    public Map analyzepollutionsource(String startTime, String endTime, String pollutantcode, String outfallid) {
        Map resMap = new HashMap();

        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";

        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime);
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endTime);
        List<String>  namelist = new ArrayList<>();
        List<String> timelist = new ArrayList<>();

        //先查询入河口信息
        BusPwkRhoutfall busPwkRhoutfall = selectBusPwkRhoutfallById(outfallid);
        namelist.add(busPwkRhoutfall.getOutfallname());
        //入河排污口数据
        BusPwkRhoutfallData busPwkRhoutfallDataParam = new BusPwkRhoutfallData();
        busPwkRhoutfallDataParam.setOutfallid(outfallid);
        busPwkRhoutfallDataParam.setStartTime(startDate);
        busPwkRhoutfallDataParam.setEndTime(endDate);
        busPwkRhoutfallDataParam.setContaminantscode(pollutantcode);
        List<BusPwkRhoutfallData> pwkRhoutfallDataList = busPwkRhoutfallDataMapper.selectBusPwkRhoutfallDataList(busPwkRhoutfallDataParam);
        if(!CollectionUtils.isEmpty(pwkRhoutfallDataList)){
            for (BusPwkRhoutfallData busPwkRhoutfallData : pwkRhoutfallDataList) {
                if(busPwkRhoutfallData.getMonitortime() == null){
                    continue;
                }
                String hourtime = DateUtils.dateFormat(busPwkRhoutfallData.getMonitortime(),"yyyy-MM-dd HH:mm");
                if(!timelist.contains(hourtime)){
                    timelist.add(hourtime);
                }
            }
        }

        //再查污染源
        List<BusPsBasWateroutput> psBasWateroutputList = new ArrayList<>();
        BasePipenetworkassociationtable basePipenetworkassociationtableParam = new BasePipenetworkassociationtable();
        basePipenetworkassociationtableParam.setRhoutfallid(outfallid);
        List<BasePipenetworkassociationtable> pipenetworkassociationtableList = basePipenetworkassociationtableMapper.selectBasePipenetworkassociationtableList(basePipenetworkassociationtableParam);
        if(!CollectionUtils.isEmpty(pipenetworkassociationtableList)){
            for (BasePipenetworkassociationtable basePipenetworkassociationtable : pipenetworkassociationtableList) {
                namelist.add(basePipenetworkassociationtable.getWspollutename()+"浓度");
                namelist.add(basePipenetworkassociationtable.getWspollutename()+"排放量");

                BusPsBasWateroutput basWateroutputParam = new BusPsBasWateroutput();
                basWateroutputParam.setPolluteid(basePipenetworkassociationtable.getWspolluteid());
                basWateroutputParam.setOutputtype("1");
                List<BusPsBasWateroutput> outputList = busPsBasWateroutputMapper.selectBusPsBasWateroutputList(basWateroutputParam);
                if(!CollectionUtils.isEmpty(outputList)){
                    psBasWateroutputList.add(outputList.get(0));
                }else{
                    BusPsBasWateroutput emptyData = new BusPsBasWateroutput();
                    emptyData.setPolluteid(basePipenetworkassociationtable.getWspolluteid());
                    psBasWateroutputList.add(emptyData);
                }
            }
        }
        Map<String,List<BusPsMidWaterhourdata>> polluDataMap = new LinkedHashMap<>();
        if(!CollectionUtils.isEmpty(psBasWateroutputList)){
            for (BusPsBasWateroutput busPsBasWateroutput : psBasWateroutputList) {
                //查询污染源数据
                List<BusPsMidWaterhourdata> datalist = new ArrayList<>();
                if(busPsBasWateroutput != null && StringUtils.isNotEmpty(busPsBasWateroutput.getOutportcode())){
                    BusPsMidWaterhourdata dataParam = new BusPsMidWaterhourdata();
                    dataParam.setOutportcode(busPsBasWateroutput.getOutportcode());
                    dataParam.setStartTime(startTime);
                    dataParam.setEndTime(endTime);
                    dataParam.setPollutantcode(pollutantcode);
                    datalist = busPsMidWaterhourdataMapper.selectBusPsMidWaterhourdataList(dataParam);
                }
                polluDataMap.put(busPsBasWateroutput.getPolluteid(),datalist);
                if(!CollectionUtils.isEmpty(datalist)){
                    for (BusPsMidWaterhourdata busPsMidWaterhourdata : datalist) {
                        if(busPsMidWaterhourdata.getMonitortime() == null){
                            continue;
                        }
                        String hourtime = DateUtils.dateFormat(busPsMidWaterhourdata.getMonitortime(),"yyyy-MM-dd HH:mm");
                        if(!timelist.contains(hourtime)){
                            timelist.add(hourtime);
                        }
                    }
                }
            }
        }

        List<List<String>> datalist = new ArrayList<>();
        List<String> pwklist = new ArrayList<>();
        for (String time : timelist) {
            String monitoring = "0";
            if(!CollectionUtils.isEmpty(pwkRhoutfallDataList)){
                for (BusPwkRhoutfallData busPwkRhoutfallData : pwkRhoutfallDataList) {
                    if(busPwkRhoutfallData.getMonitortime() == null){
                        continue;
                    }
                    String hourtime = DateUtils.dateFormat(busPwkRhoutfallData.getMonitortime(),"yyyy-MM-dd HH:mm");
                    if(time.equals(hourtime)){
                        monitoring = busPwkRhoutfallData.getMonitoring();
                        break;
                    }
                }
            }
            pwklist.add(monitoring);
        }
        datalist.add(pwklist);

        if(!CollectionUtils.isEmpty(psBasWateroutputList)){
            for (BusPsBasWateroutput busPsBasWateroutput : psBasWateroutputList) {
                List<String> strengthdatalist = new ArrayList<>();
                List<String> flowdatalist = new ArrayList<>();
                List<BusPsMidWaterhourdata> hourdatalist = polluDataMap.get(busPsBasWateroutput.getPolluteid());
                Double strength = 0d;
                Double flow = 0d;
                for (String time : timelist) {
                    for (BusPsMidWaterhourdata busPsMidWaterhourdata : hourdatalist) {
                        if(busPsMidWaterhourdata.getMonitortime() == null){
                            continue;
                        }
                        String hourtime = DateUtils.dateFormat(busPsMidWaterhourdata.getMonitortime(),"yyyy-MM-dd HH:mm");
                        if(hourtime.equals(time)){
                            if(busPsMidWaterhourdata.getAvgstrength() != null){
                                strength = busPsMidWaterhourdata.getAvgstrength();
                            }
                            if(busPsMidWaterhourdata.getAvgflow() != null){
                                flow = busPsMidWaterhourdata.getAvgflow();
                            }
                            break;
                        }
                    }
                    strengthdatalist.add(strength.toString());
                    flowdatalist.add(flow.toString());
                }
                datalist.add(strengthdatalist);
                datalist.add(flowdatalist);
            }
        }

        resMap.put("datalist",datalist);
        resMap.put("namelist",namelist);
        resMap.put("timelist",timelist);
        return resMap;
    }

    @Override
    public Map analyzesegment(String startTime, String endTime, String pollutantcode, String outfallid) {
        Map resMap = new HashMap();

        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";

        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime);
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endTime);
        List<String>  namelist = new ArrayList<>();
        List<String> timelist = new ArrayList<>();

        //先查询入河口信息
        BusPwkRhoutfall busPwkRhoutfall = selectBusPwkRhoutfallById(outfallid);
        namelist.add(busPwkRhoutfall.getOutfallname());
        //入河排污口数据
        String pollutantname = "";
        BusPwkRhoutfallData busPwkRhoutfallDataParam = new BusPwkRhoutfallData();
        busPwkRhoutfallDataParam.setOutfallid(outfallid);
        busPwkRhoutfallDataParam.setStartTime(startDate);
        busPwkRhoutfallDataParam.setEndTime(endDate);
        busPwkRhoutfallDataParam.setContaminantscode(pollutantcode);
        List<BusPwkRhoutfallData> pwkRhoutfallDataList = busPwkRhoutfallDataMapper.selectBusPwkRhoutfallDataList(busPwkRhoutfallDataParam);
        if(!CollectionUtils.isEmpty(pwkRhoutfallDataList)){
            for (BusPwkRhoutfallData busPwkRhoutfallData : pwkRhoutfallDataList) {
                if(StringUtils.isNotEmpty(busPwkRhoutfallData.getContaminants()) && StringUtils.isEmpty(pollutantname)){
                    pollutantname = busPwkRhoutfallData.getContaminants();
                }
                if(busPwkRhoutfallData.getMonitortime() == null){
                    continue;
                }
                String hourtime = DateUtils.dateFormat(busPwkRhoutfallData.getMonitortime(),"yyyy-MM-dd HH:mm");
                if(!timelist.contains(hourtime)){
                    timelist.add(hourtime);
                }
            }
        }

        //再查断面
        List<BusWaterSegment> busWaterSegmentList = new ArrayList<>();
        BusWaterSegmentorrhoutfall busWaterSegmentorrhoutfallParam = new BusWaterSegmentorrhoutfall();
        busWaterSegmentorrhoutfallParam.setRhoutfallid(outfallid);
        List<BusWaterSegmentorrhoutfall> busWaterSegmentorrhoutfallList = busWaterSegmentorrhoutfallMapper.selectBusWaterSegmentorrhoutfallList(busWaterSegmentorrhoutfallParam);
        if(!CollectionUtils.isEmpty(busWaterSegmentorrhoutfallList)){
            for (BusWaterSegmentorrhoutfall busWaterSegmentorrhoutfall : busWaterSegmentorrhoutfallList) {
                if("0".equals(busWaterSegmentorrhoutfall.getUpstreamordownstream())){
                    namelist.add(busWaterSegmentorrhoutfall.getSegmentname()+"(下游)");
                }else if("1".equals(busWaterSegmentorrhoutfall.getUpstreamordownstream())){
                    namelist.add(busWaterSegmentorrhoutfall.getSegmentname()+"(上游)");
                }else{
                    namelist.add(busWaterSegmentorrhoutfall.getSegmentname());
                }

                if(StringUtils.isNotEmpty(busWaterSegmentorrhoutfall.getSegmentid())){
                    BusWaterSegment busWaterSegment = busWaterSegmentMapper.selectBusWaterSegmentByID(busWaterSegmentorrhoutfall.getSegmentid());
                    if(busWaterSegment != null){
                        busWaterSegmentList.add(busWaterSegment);
                    }else{
                        busWaterSegment = new BusWaterSegment();
                    }
                    if(busWaterSegment != null){
                        busWaterSegmentList.add(busWaterSegment);
                    }else{
                        busWaterSegment = new BusWaterSegment();
                        busWaterSegment.setId(busWaterSegmentorrhoutfall.getSegmentid());
                        busWaterSegmentList.add(busWaterSegment);
                    }
                }
            }
        }

        Map<String,List<BusWaterAutomonitorHourdata>> segmentDataMap = new LinkedHashMap<>();
        if(!CollectionUtils.isEmpty(busWaterSegmentList)){
            for (BusWaterSegment busWaterSegment : busWaterSegmentList) {
                //查询污染源数据
                List<BusWaterAutomonitorHourdata> datalist = new ArrayList<>();
                if(busWaterSegment != null && StringUtils.isNotEmpty(busWaterSegment.getId())){
                    BusWaterAutomonitorHourdata dataParam = new BusWaterAutomonitorHourdata();
                    dataParam.setSegmentid(busWaterSegment.getId());
                    dataParam.setStartTime(startDate);
                    dataParam.setEndTime(endDate);
                    dataParam.setItemnameeq(pollutantname);
                    datalist = busWaterAutomonitorHourdataMapper.selectBusWaterAutomonitorHourdataList(dataParam);
                }
                segmentDataMap.put(busWaterSegment.getId(),datalist);
                if(!CollectionUtils.isEmpty(datalist)){
                    for (BusWaterAutomonitorHourdata hourdata : datalist) {
                        if(hourdata.getSampletime() == null){
                            continue;
                        }
                        String hourtime = DateUtils.dateFormat(hourdata.getSampletime(),"yyyy-MM-dd HH:mm");
                        if(!timelist.contains(hourtime)){
                            timelist.add(hourtime);
                        }
                    }
                }
            }
        }

        List<List<String>> datalist = new ArrayList<>();
        List<String> pwklist = new ArrayList<>();
        for (String time : timelist) {
            String monitoring = "0";
            if(!CollectionUtils.isEmpty(pwkRhoutfallDataList)){
                for (BusPwkRhoutfallData busPwkRhoutfallData : pwkRhoutfallDataList) {
                    if(busPwkRhoutfallData.getMonitortime() == null){
                        continue;
                    }
                    String hourtime = DateUtils.dateFormat(busPwkRhoutfallData.getMonitortime(),"yyyy-MM-dd HH:mm");
                    if(time.equals(hourtime)){
                        monitoring = busPwkRhoutfallData.getMonitoring();
                        break;
                    }
                }
            }
            pwklist.add(monitoring);
        }
        datalist.add(pwklist);

        if(!CollectionUtils.isEmpty(busWaterSegmentList)){
            for (BusWaterSegment busWaterSegment : busWaterSegmentList) {
                List<String> sourcevaluelist = new ArrayList<>();
                List<BusWaterAutomonitorHourdata> hourdatalist = segmentDataMap.get(busWaterSegment.getId());
                String sourcevalue = "0";
                for (String time : timelist) {
                    for (BusWaterAutomonitorHourdata hourdata : hourdatalist) {
                        if(hourdata.getSampletime() == null){
                            continue;
                        }
                        String hourtime = DateUtils.dateFormat(hourdata.getSampletime(),"yyyy-MM-dd HH:mm");
                        if(hourtime.equals(time)){
                            if(StringUtils.isNotEmpty(hourdata.getSourcevalue())){
                                sourcevalue = hourdata.getSourcevalue();
                            }

                            break;
                        }
                    }
                    sourcevaluelist.add(sourcevalue);
                }
                datalist.add(sourcevaluelist);
            }
        }
        resMap.put("datalist",datalist);
        resMap.put("namelist",namelist);
        resMap.put("timelist",timelist);
        return resMap;
    }

    /**
     * 删除入河排污口信息
     * 
     * @param id 入河排污口主键
     * @return 结果
     */
    @Override
    public int deleteBusPwkRhoutfallById(String id)
    {
        return busPwkRhoutfallMapper.deleteBusPwkRhoutfallById(id);
    }
}
